package com.ruoyi.stuclient.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.stuclient.domain.EwcStudent;
import com.ruoyi.stuclient.domain.dto.SMSGetDto;
import com.ruoyi.stuclient.domain.dto.StudentResetPwdDto;
import com.ruoyi.stuclient.domain.vo.StudentLoginVo;
import com.ruoyi.stuclient.mapper.EwcStudentMapper;
import com.ruoyi.stuclient.service.IEwcStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 学生信息Service业务层处理
 * 
 * @author lcj
 * @date 2025-02-27
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EwcStudentServiceImpl extends MPJBaseServiceImpl<EwcStudentMapper, EwcStudent> implements IEwcStudentService {

    @Autowired
    private  EwcStudentMapper ewcStudentMapper;

    @Override
    public boolean resetLoginPassword(StudentResetPwdDto studentResetPwdDto, String newPassword) {
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcStudent> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("student_no", studentResetPwdDto.getStudentNo());
        // 设置要更新的字段及其值
        Date nowDate = DateUtils.getNowDate();
        EwcStudent entity = new EwcStudent();
        entity.setPassword(newPassword);
        entity.setUpdateTime(nowDate);
        // 执行更新操作
        return this.update(entity,updateWrapper);
    }

    @Override
    public EwcStudent getStudentInfoByStudentNo(String studentNo) {
        EwcStudent ewcStudent = this.getOne(Wrappers.<EwcStudent>lambdaQuery().eq(EwcStudent::getStudentNo, studentNo));
//                .ne(EwcStudent::getStatus, 0));
        if(Objects.nonNull(ewcStudent)){
            ewcStudent.setPassword("");
        }
        return ewcStudent;
    }

    @Override
    public String getClientMobileNo(SMSGetDto smsGetDto) {
        QueryWrapper<EwcStudent> wrapper = new QueryWrapper<>();
        wrapper.select("mobile_no")
                .eq("student_no", smsGetDto.getClientNo());
        return (String) this.getOne(wrapper).getMobileNo();
    }

    @Override
    public StudentLoginVo getLoginInfoByStudentNo(String studentNo) {
        return ewcStudentMapper.getLoginInfoByStudentNo(studentNo);
    }

    @Override
    public EwcStudent getStudentSettingInfo() {
        EwcStudent ewcStudent = this.getOne(Wrappers.<EwcStudent>lambdaQuery().eq(EwcStudent::getId, ThirdSessionHolder.getThirdSession().getId()));
        if(Objects.nonNull(ewcStudent)){
            ewcStudent.setPassword("");
        }
        return ewcStudent;
    }

    @Override
    public boolean resetStudentSettingInfo(EwcStudent studentInfo) {
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcStudent> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("id", ThirdSessionHolder.getThirdSession().getId());
        // 设置要更新的字段及其值
        Date nowDate = DateUtils.getNowDate();
        EwcStudent entity = new EwcStudent();
        entity.setMobileNo(studentInfo.getMobileNo());
        entity.setStudentHeight(studentInfo.getStudentHeight());
        entity.setStudentWeight(studentInfo.getStudentWeight());
        entity.setAvatarPath(studentInfo.getAvatarPath());
        entity.setUpdateTime(nowDate);
        // 执行更新操作
        return this.update(entity,updateWrapper);
    }
}
